package com.yl.ydk.presenter.impl

import android.content.Context
import android.content.Intent
import androidx.fragment.app.Fragment
import com.yl.ydk.R
import com.yl.ydk.api.Api
import com.yl.ydk.base.BaseFragmentPageAdapter
import com.yl.ydk.base.BasePresenter
import com.yl.ydk.bean.HealthtestQuestionBean
import com.yl.ydk.bean.TestFinishBean
import com.yl.ydk.presenter.contract.HealthtestQuestionActivityContract
import com.yl.ydk.ui.activity.TestFinishActivity
import com.yl.ydk.ui.fragment.HealthtestQuestionFragment
import com.google.gson.Gson
import okhttp3.RequestBody
import rx.Observer
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers
import java.util.ArrayList

class HealthtestQuestionActivityImpl : BasePresenter<HealthtestQuestionActivityContract.View>(),
    HealthtestQuestionActivityContract.Presenter<HealthtestQuestionActivityContract.View> {

    private var mAdapter: BaseFragmentPageAdapter? = null
    private val mTitleList = ArrayList<String>()
    private val mFragments = ArrayList<Fragment>()
    private  var  healthtestQuestionBean:HealthtestQuestionBean? = null

    override fun getHealthtestQuestion(context: Context, body: RequestBody?) {
        showLoadingDialog(context)
        addSubscrebe(
            Api.getInstance()?.getHealthtestQuestion(body)
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribeOn(Schedulers.io())
                ?.unsubscribeOn(Schedulers.io())
                ?.subscribe(object : Observer<HealthtestQuestionBean?> {
                    override fun onCompleted() {}
                    override fun onError(e: Throwable) {
                        dismissLoadingDialog()
                        mView?.showError(e.toString())
                    }

                    override fun onNext(t: HealthtestQuestionBean?) {
                        dismissLoadingDialog()
                        if (t?.data!= null && t.code == 1) {
                            healthtestQuestionBean = t
                            t.data.item.forEach {
                                mFragments.add(HealthtestQuestionFragment.getInstance(it))
                            }

                            mAdapter = BaseFragmentPageAdapter(
                                mView?.getActivity()?.supportFragmentManager,
                                mFragments, mTitleList
                            )

                            mView?.getVpQuestion()?.adapter = mAdapter
                            mView?.getVpQuestion()?.setNoScroll(true)
                            mView?.getFragmentSize(mFragments.size)
                            mView?.getSubjectId(t.data.subject.question_id)

                        } else {
                            mView?.showError(context.getString(R.string.connectFailure))
                        }
                    }
                })
        )
    }

    override fun getTestFinish(context: Context, body: RequestBody?) {
        showLoadingDialog(context)
        addSubscrebe(
            Api.getInstance()?.getTestFinish(body)
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribeOn(Schedulers.io())
                ?.unsubscribeOn(Schedulers.io())
                ?.subscribe(object : Observer<TestFinishBean?> {
                    override fun onCompleted() {}
                    override fun onError(e: Throwable) {
                        dismissLoadingDialog()
                        mView?.showError(e.toString())
                    }

                    override fun onNext(t: TestFinishBean?) {
                        dismissLoadingDialog()
                        if (t?.data != null && t.code == 1) {
                            mView?.getActivity()?.let {
                                val intent = Intent(it, TestFinishActivity::class.java)
                                intent.putExtra("data", Gson().toJson(t))
                                intent.putExtra("healthtestQuestionBean",Gson().toJson(healthtestQuestionBean))
                                it.startActivityIn(intent,it)
                                it.finish()
                            }
                        } else {
                            mView?.showError(context.getString(R.string.connectFailure))
                        }
                    }
                })
        )
    }

}